layui.define(['table', 'form', 'admin'], function (exports) {
    var $ = layui.$
        , table = layui.table

    // 渠道列表
    table.render({
        elem: '#LAY-regsource-manage'
        , url: '/guild/regsource/getlist'
        , cols: [[
            { field: 'source_key', width: 180, title: '渠道标识', fixed: true }
            , { field: 'source_name', title: '渠道名称' }
            , { field: 'url', title: '渠道域名' }
            , {
                field: 'down_total', title: '下载数量', templet: function (d) {
                    return d.appdownload ? d.appdownload.total : 0;
                }
            },
            { field: 'reg_count', width: 180, title: '注册人数' },
            // { field: 'reg_count', width: 180, title: 'IP注册转换' }
            , {
                field: '', title: '设备', templet: function (d) {
                    switch (d.device) {
                        case 1:
                            return "<span style='color:#009688'>安卓</span>";
                        case 2:
                            return "<span style='color:#1E9FFF'>苹果</span>";
                        case 3:
                            return "<span style='color:#FFB800'>PC端</span>";
                    }
                    return '';
                }
            }
            , { field: 'create_time', width: 180, title: '创建时间' },
            { title: '操作', width: 220, align: 'center', fixed: 'right', toolbar: '#table-regsource-operate' }
        ]]
        , page: true
        , limit: 20
        , height: 'full-140'
        , text: '对不起，加载出现异常！'
        , even: true
    });

    //渠道列表工具条监听
    table.on('tool(LAY-regsource-manage)', function (obj) {
        if (obj.event === 'everyday') {
            let index = layer.open({
                type: 2
                , title: '渠道每日数据'
                , content: '/guild/regsource/everyday?source_key=' + obj.data.source_key
                , maxmin: true
                , area: ['700px', '500px']
            });
            layer.full(index);
        }
    });

    // 渠道每日数据列表
    table.render({
        elem: '#LAY-source-everyday-manage'
        , url: '/guild/regsource/everyday_list?source_key=' + $('#sourceKey').val()
        , totalRow: true
        , where: {
            type: $('.timeChoice .btn_danger').data('type')
            , start_time: $('#start_time_input').val()
            , end_time: $('#end_time_input').val()
        }
        , cols: [[
            { field: 'date', width: 120, title: '日期', totalRowText: '' }
            , {
                field: '', title: '渠道标识', minWidth: 120, templet: function (d) {
                    return d.regsource ? d.regsource.source_key : '';
                }
            }
            , {
                field: '', title: '渠道名称', minWidth: 100, templet: function (d) {
                    return d.regsource ? d.regsource.source_name : '';
                }
            }
            , { field: 'dlcount', width: 200, title: '新增下载次数', totalRowText: '' }
            , { field: 'register', width: 200, title: '新增注册人数', totalRowText: '' }
            , { field: 'valid_user', width: 200, title: '新增有效用户', totalRowText: '' }
            // , { field: 'loggin_user', width: 200, title: '当日登录用户', totalRowText: '' }
            , { field: 'active_user', width: 200, title: '活跃用户', totalRowText: '' }
            , {
                field: '', title: '次日留存率', width: 150, templet: function (d) {
                    return d.retention + '%'
                }
            }
        ]]
        //主要利用回调渲染
        , done: function (res, curr, count) {
            if (res.statis) {
                var total_count = '<strong>共 <span style="color:#1E9FFF;">' + res.statis.count_days + '</span> 天统计</strong>'
                this.elem.next().find('.layui-table-total td[data-field="date"] .layui-table-cell').html(total_count);
                this.elem.next().find('.layui-table-total td[data-field="dlcount"] .layui-table-cell').text(res.statis.dl_count);
                this.elem.next().find('.layui-table-total td[data-field="register"] .layui-table-cell').text(res.statis.register);
                this.elem.next().find('.layui-table-total td[data-field="valid_user"] .layui-table-cell').text(res.statis.valid_user);
                // this.elem.next().find('.layui-table-total td[data-field="loggin_user"] .layui-table-cell').text(res.statis.loggin_user);
                this.elem.next().find('.layui-table-total td[data-field="active_user"] .layui-table-cell').text(res.statis.active_user);

            }
        }
        , page: true
        , limit: 20
        , height: 'full-140'
        , text: '对不起，加载出现异常！'
        , even: true
    });

    exports('guild/regsource', {})
});